﻿using System.Text.Json.Serialization;
using FanaticRock.MediaSource.BilibiliAPI.Enums;
using FanaticRock.MediaSource.BilibiliAPI.Extensions;
using FanaticRock.MediaSource.BilibiliAPI.Helpers;

namespace FanaticRock.MediaSource.BilibiliAPI.RequestVo.Player.Wbi
{
    ///<summary>
    /// 来源
    /// https://socialsisteryi.github.io/bilibili-API-collect/docs/video/videostream_url.html#%E8%8E%B7%E5%8F%96%E8%A7%86%E9%A2%91%E6%B5%81%E5%9C%B0%E5%9D%80-web%E7%AB%AF
    /// </summary>
    public class PlayUrlRequestVo
    {
        [JsonPropertyName("avid")]
        public long Avid { get; set; }

        [JsonPropertyName("bvid")]
        public string Bvid { get; set; }

        [JsonPropertyName("cid")]
        public long Cid { get; set; }

        ///<summary>视频清晰度</summary>
        [JsonPropertyName("qn")]
        public QnEnum Qn { get; set; } = QnEnum.Qn_360P;

        [JsonPropertyName("fnval")]
        public FnvalEnum Fnval { get; set; } = FnvalEnum.Fnval_MP4;

        [JsonPropertyName("fnver")]
        public int Fnver { get; set; } = 0;

        [JsonPropertyName("fourk")]
        public FourkEnum Fourk { get; set; } = FourkEnum.Fourk_1080P;

        [JsonPropertyName("session")]
        public string Session { get; set; }

        [JsonPropertyName("otype")]
        public string Otype { get; set; }

        ///<summary>参数来自  <see cref="PlatformEnum"/>  </summary>
        [JsonPropertyName("platform")]
        public string Platform { get; set; }

        [JsonPropertyName("high_quality")]
        public int HighQuality { get; set; } = 0;


        public async Task<string> ToWbiParameter()
        {
            if (Avid == 0)
            {
                var map = await WbiHelper.EncWbi(new Dictionary<string, string>()
                {
                    {"bvid",  Bvid },
                    {"cid", Cid.ToString() },
                    {"fnval", ((int)Fnval).ToString() },
                });
                return await map.ToQueryParameterFromMap();
            }
            else if (string.IsNullOrWhiteSpace(Bvid))
            {
                var map = await WbiHelper.EncWbi(new Dictionary<string, string>()
                {
                    {"avid",  Avid.ToString() },
                    {"cid", Cid.ToString() },
                    {"fnval", ((int)Fnval).ToString() },
                });

                return await map.ToQueryParameterFromMap();
            }

            return string.Empty;
        }
    }
}
